<?php
namespace App\Http\Controllers\v1;

use Illuminate\Http\Request;
use Mockery\Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\RequestBackController;
use App\Http\Controllers\Controller;
use App\Http\Controllers\CheckController;
/*
   案由
*/
class ActionCenterController extends Controller{
	
	
	/*
    案由显示
    */
    public function ShowActionByType($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','CaseTypeId'])){//案由所属类型
            return RequestBackController::Structure(502);
        }else{

                $row = DB::connection('mysql_other')->table('yl_case_action')->select('case_action_id as CaseActionId','case_action_title as CaseActionTitle','case_action_pid as CaseActionPid','case_type_id as CaseTypeId')
                                                    ->where('case_action_Pid',0)
                                                    ->where('case_type_id',$pos['CaseTypeId'])
                                                    ->get()->map(function ($val){
                                                                    $val = (array)$val;
                                                                    return $val;
                                                                })

                                                    ->toArray();
                if($row){
                    return RequestBackController::Structure(501,$row);
                }else{
                    return RequestBackController::Structure(504);
                }
            
        }

    }
	 /*细节案由模板显示*/
    public function ShowDetailByAction($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','CaseActionId'])){//细节案件模板所属案由
            return RequestBackController::Structure(502);
        }else{

            $row = DB::connection('mysql_other')->table('yl_case_action')->select('case_action_id as CaseActionId','case_action_title as CaseActionTitle','case_action_pid as CaseActionPid','case_type_id as CaseTypeId')
                                                    ->where('case_action_pid',$pos['CaseActionId'])
                                                    ->get()->map(function ($val){
                                                                    $val = (array)$val;
                                                                    return $val;
                                                                })

                                                    ->toArray();
            if($row){
                return RequestBackController::Structure(501,$row);
            }else{
                return RequestBackController::Structure(504);
            }
            
        }

    }
    /*
	案由模板显示
	*/
    public function ShowAction($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId'])){//
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $row = DB::connection('mysql_other')->table('yl_case_action as a')->select('a.case_action_id as CaseActionId','a.case_action_title as CaseActionTitle','a.case_action_pid as CaseActionPid')
                                                    ->where('a.case_action_Pid','=',0)
                                                    ->get()->map(function ($val){
                                                                    $val = (array)$val;
                                                                    return $val;
                                                                })

                                                    ->toArray();
                if($row){
                    return RequestBackController::Structure(501,$row);
                }else{
                    return RequestBackController::Structure(504);
                }
            }else{
                return RequestBackController::Structure(403);
            }
        }

    }
  
     /*
    新建案由
    */
    public function AddAction($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        
        if(!CheckController::CheckParameter($pos,['UserId','CaseActionTitle','CaseTypeId'])){//
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $add = array();
                $add['case_action_title'] =$pos['CaseActionTitle'];
                $add['case_type_id'] =$pos['CaseTypeId'];
                $add['case_action_pid'] =0;
                $row = DB::connection('mysql_other')->table('yl_case_action')->insert($add);
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }
            }else{
                return RequestBackController::Structure(403);
            }
            

        }
        
        
    }

     /*
    编辑案由
    */
    public function EditAction($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','CaseActionId','CaseActionTitle'])){//
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $map = array();
                $map['case_action_title'] =$pos['CaseActionTitle'];
                $row = DB::connection('mysql_other')->table('yl_case_action')->where('case_action_id',$pos['CaseActionId'])->update($map);
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }
            }else{
                return RequestBackController::Structure(403);
            }
            

        }

    }
	
     /*
    删除案由
    */
    public function DeleteAction($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','CaseActionId'])){//
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                //查询该id是否存在
                $Action = DB::connection('mysql_other')->table('yl_case_action')->where('case_action_id',$pos['CaseActionId'])->first();
                //查询是否存在下一级：
                if($Action){
                    $NextAction = DB::connection('mysql_other')->table('yl_case_action')->where('case_action_pid',$pos['CaseActionId'])->first();
                }
                if($NextAction){//存在下一级无法删除
                    return RequestBackController::Structure(607);
                }else{
                    $row=DB::connection('mysql_other')->table('yl_case_action')->where('case_action_id',$pos['CaseActionId'])->delete();
                    if($row){
                        return RequestBackController::Structure(601);
                    }else{
                        return RequestBackController::Structure(602);
                    }
                }
            }else{
                return RequestBackController::Structure(403);
            }
        } 
        
    }
	/*
    新建细节模板
    */
    public function AddActionDetail($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        
        if(!CheckController::CheckParameter($pos,['UserId','CaseActionDetailTitle','CaseActionDetailId'])){//CaseActionId父级案由id
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $add = array();
                $add['case_action_title'] =$pos['CaseActionDetailTitle'];
                $add['case_action_pid'] =$pos['CaseActionDetailId'];
                $row = DB::connection('mysql_other')->table('yl_case_action')->insert($add);
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }
            }else{
                return RequestBackController::Structure(403);
            }
        } 
    }
	/*
    编辑细节案由
    */
    public function EditActionDetail($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','CaseActionId','CaseActionTitle'])){//
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $map = array();
                $map['case_action_title'] =$pos['CaseActionTitle'];
                $row = DB::connection('mysql_other')->table('yl_case_action')->where('case_action_id',$pos['CaseActionId'])->update($map);
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }
            }else{
                return RequestBackController::Structure(403);
            }
            

        }

    }
	  /*
    删除细节案由
    */
    public function DeleteActionDetail($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','CaseActionId'])){//
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $Action = DB::connection('mysql_other')->table('yl_case_action')->where('case_action_id',$pos['CaseActionId'])->first();
                if($Action){
                    $row=DB::connection('mysql_other')->table('yl_case_action')->where('case_action_id',$pos['CaseActionId'])->delete();
                    if($row){
                        return RequestBackController::Structure(601);
                    }else{
                        return RequestBackController::Structure(602);
                    }
                }
            }else{
                return RequestBackController::Structure(403);
            }
        } 
        
    }
	/*联动显示数组*/
	protected function ListClassified($List,$Fid,$ParentColumn,$ChildColumn){
        $arr=array();
        foreach($List as $k=>$v)
        {
            if($v[$ParentColumn]==$Fid)
            {
                $arr[$v[$ChildColumn]]=$v;
            }
        }
        if(!empty($arr))
        {
            foreach($arr as $k=>$v)
            {
                $temp=$this->ListClassified($List,$v[$ChildColumn],$ParentColumn,$ChildColumn);
                if(!empty($temp))
                {
                    $arr[$k]['list']=array_values($temp);
                }
            }
        }
        return $arr;
    }
	/*案由和案由细节的联动显示*/
	public function ShowActionList($request){
		if(!CheckController::CheckUser($request)){
			return RequestBackController::Structure(205);
		}
		$pos=$request->all();
		if(!CheckController::CheckParameter($pos,['UserId'])){//
			return RequestBackController::Structure(502);
		}else{
			$Purview = new PurviewController();
			$UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
			if($UserPurview == 'All'){
				$row=DB::connection('mysql_other')->table('yl_case_action')->select('case_action_id as CaseActionId','case_action_title as CaseActionTitle','case_action_pid as CaseActionPid')
											->get()->map(function ($val){
													$val = (array)$val;
														return $val;
													})
													->toArray();
				$options = $this->ListClassified($row,0,'CaseActionPid','CaseActionId');
				if($options){
					return RequestBackController::Structure(501,$options);
				}else{
					return RequestBackController::Structure(504);
				}
			}else{
				return RequestBackController::Structure(403);
			}
		}


	}

	/*
    新建细节模板下的模板名
    */
    public function ActionDetailModelAdd($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        
        if(!CheckController::CheckParameter($pos,['UserId','CaseActionDetailTitle','CaseActionId'])){//
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $add = array();
                $add['case_action_detail_title'] =$pos['CaseActionDetailTitle'];
                $add['case_action_id'] =$pos['CaseActionId'];
                $row = DB::connection('mysql_other')->table('yl_case_action_detail')->insert($add);
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }
            }else{
                return RequestBackController::Structure(403);
            }
            

        }

    }
	/*
    修改细节模板下的模板名
    */
    public function EditActionDetailTitle($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        
        if(!CheckController::CheckParameter($pos,['UserId','CaseActionDetailTitle','CaseActionDetailId'])){//
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $up = array();
                $up['case_action_detail_title'] =$pos['CaseActionDetailTitle'];
                $row = DB::connection('mysql_other')->table('yl_case_action_detail')->where('case_action_detail_id',$pos['CaseActionDetailId'])->update($up);
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }
            }else{
                return RequestBackController::Structure(403);
            }
            

        }

    }
    /*
    编辑细节案由模板下的模板内容
    */
    public function ActionDetailModelEdit($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','CaseActionDetailId','CaseActionDetailContent'])){//
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
				$Findrow = DB::connection('mysql_other')->table('yl_case_action_detail')->where('case_action_detail_id',$pos['CaseActionDetailId'])->first();
				if(json_encode($pos['CaseActionDetailContent']) == $Findrow->case_action_detail_content){
					
					return RequestBackController::Structure(601);
					
				}
                $map = array();
                $map['case_action_detail_content'] =json_encode($pos['CaseActionDetailContent']);
                $row = DB::connection('mysql_other')->table('yl_case_action_detail')->where('case_action_detail_id',$pos['CaseActionDetailId'])->update($map);
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }
            }else{
                return RequestBackController::Structure(403);
            }
            

        }

    }
    /*
    删除细节模板下的模板内容
    */
    public function ActionDetailDelete($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','CaseActionDetailId'])){//
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                    $row=DB::connection('mysql_other')->table('yl_case_action_detail')->where('case_action_detail_id',$pos['CaseActionDetailId'])->delete();
                    if($row){
                        return RequestBackController::Structure(601);
                    }else{
                        return RequestBackController::Structure(602);
                    }
            
            }else{
                return RequestBackController::Structure(403);
            }
        } 
    }

  /*
    显示细节模板下的模板内容
    */
    public function ShowModeByDetailList($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','CaseActionId'])){//案由所属类型
            return RequestBackController::Structure(502);
        }else{

            $row = DB::connection('mysql_other')->table('yl_case_action_detail')->select('case_action_detail_id as CaseActionDetailId','case_action_detail_title as CaseActionDetailTitle','case_action_id as CaseActionId','case_action_detail_content as CaseActionDetailContent')
                                                    ->where('case_action_id',$pos['CaseActionId'])
                                                    ->get()->map(function ($val){
                                                                    $val = (array)$val;
                                                                    return $val;
                                                                })

                                                    ->toArray();
            foreach ($row as $k => $v) {
               $row[$k]['CaseActionDetailContent']=json_decode($v['CaseActionDetailContent']);
            }
            if($row){
                return RequestBackController::Structure(501,$row);
            }else{
                return RequestBackController::Structure(504);
            }
            
        }

    }
	
	 /*
    显示细节模板下的单个模板内容
    */
    public function ShowModeByDetail($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','CaseActionDetailId'])){//案由所属类型
            return RequestBackController::Structure(502);
        }else{

            $row = DB::connection('mysql_other')->table('yl_case_action_detail')->select('case_action_detail_id as CaseActionDetailId','case_action_detail_title as CaseActionDetailTitle','case_action_id as CaseActionId','case_action_detail_content as CaseActionDetailContent')
                                                    ->where('case_action_detail_id',$pos['CaseActionDetailId'])
                                                    ->first();
													//print_r($row);die();
            $row->CaseActionDetailContent=json_decode($row->CaseActionDetailContent);
            if($row){
                return RequestBackController::Structure(501,$row);
            }else{
                return RequestBackController::Structure(504);
            }
            
        }

    }










   
}